#include "GameMenuDlg.h"
#include "GameScene.h"

using namespace cocos2d;

#define MENU_TAG_RESUME		0
#define MENU_TAG_MAIN		1
#define MENU_TAG_OPTION		2
#define MENU_TAG_EXIT		3

#define LINE_SPACE	40

GameMenuDlg::~GameMenuDlg(void)
{
}

// on "init" you need to initialize your instance
bool GameMenuDlg::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////
		
		/*
        // 1. Add a menu item with "X" image, which is clicked to quit the program.
		
        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);
		*/

		// Get window size
        CCSize size = CCDirector::sharedDirector()->getWinSize();

		this->setTouchEnabled(true);
		// add labeled menu
		// resume menu
		CCLabelTTF* resumeLabel = CCLabelTTF::create("Resume", "Arial", 24);
		CCMenuItemLabel* pResumeMenuItem = CCMenuItemLabel::create(resumeLabel, this, menu_selector(GameMenuDlg::menuCallback));
		pResumeMenuItem->setTag(MENU_TAG_RESUME);
		pResumeMenuItem->setPosition( CCPointMake( size.width / 2, (size.height - 240 - LINE_SPACE) ));
		// restart menu
		CCLabelTTF* mainLabel = CCLabelTTF::create("Main menu", "Arial", 24);
		CCMenuItemLabel* pMainMenuItem = CCMenuItemLabel::create(mainLabel, this, menu_selector(GameMenuDlg::menuCallback));
		pMainMenuItem->setTag(MENU_TAG_MAIN);
		pMainMenuItem->setPosition( CCPointMake( size.width / 2, (size.height - 240 - LINE_SPACE * 2) ));
		// main menu
		CCLabelTTF* optionLabel = CCLabelTTF::create("Option", "Arial", 24);
		CCMenuItemLabel* pOptionMenuItem = CCMenuItemLabel::create(optionLabel, this, menu_selector(GameMenuDlg::menuCallback));
		pOptionMenuItem->setTag(MENU_TAG_OPTION);
		pOptionMenuItem->setPosition( CCPointMake( size.width / 2, (size.height - 240 - LINE_SPACE * 3) ));
		// exit menu
		CCLabelTTF* exitLabel = CCLabelTTF::create("Exit", "Arial", 24);
		CCMenuItemLabel* pExitMenuItem = CCMenuItemLabel::create(exitLabel, this, menu_selector(GameMenuDlg::menuCallback));
		pExitMenuItem->setTag(MENU_TAG_EXIT);
		pExitMenuItem->setPosition( CCPointMake( size.width / 2, (size.height - 240 - LINE_SPACE * 4) ));

        CCMenu* pMenu = CCMenu::create(pResumeMenuItem, pMainMenuItem, pOptionMenuItem, pExitMenuItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to ManiMenuScene layer as a child layer.
        this->addChild(pMenu, 1);

        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("Images/bg_gamemenu.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
		pSprite->setAnchorPoint(ccp(0.5f, 0.5f));
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

        bRet = true;
    } while (0);

    return bRet;
}

void GameMenuDlg::menuCallback(CCObject* pSender)
{
    // "close" menu item clicked
	CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);

	switch (pMenuItem->getTag())
	{
		case MENU_TAG_RESUME :
			{
				m_pGame->resumeGame();
			}
			break;

		case MENU_TAG_MAIN :
			{
				m_pGame->moveToMainMenu();
			}
			break;

		case MENU_TAG_OPTION :
			{
				m_pGame->moveToOptionMenu();
			}
			break;

		case MENU_TAG_EXIT :
			CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
			exit(0);
#endif
			break;
	}
}

void GameMenuDlg::SetParentLayer(GameScene *pParent)
{
	m_pGame = pParent;
}
